<?php

namespace App\models;

use houdunwang\arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = ['title','parent_id','icon'];
    public function getAllCategory(){
//        $categories = Arr::category(self::get());
//        获取分类树状结构
        $categories = $this->getTreeCategory();
//        dd($categories);
//        foreach ($categories as $k=>$v){
//            $v['is_parent']=$v['id']==$this['parent_id'];
//            $v['is_child']=Arr::isChild($categories, $v['id'], $this['id'],'id', 'parent_id');
//            $categories[$k]=$v;
//        }
//定义一个变量用来接收当前分类数据
         $nowCategory = [];
        foreach ($categories as $category)
        {
//            dd($category);
            if ($category['id'] == $this['id'])
            {
                $nowCategory = $category;
            }
        }
        foreach ($categories as $k=>$category)
        {
//            dd($category);
//            添加字段只能这样写：$categories[$k]['_disable']= '';
            if ($category['_level'] >= $nowCategory['_level'])
            {
                $categories[$k]['_disabled'] = 'disabled';
            }else{
                $categories[$k]['_disabled'] = '';
            }
            if ($category['id'] == $nowCategory['parent_id'])
            {
                $categories[$k]['_selected'] = 'selected';
            }else{
                $categories[$k]['_selected'] = '';
            }
        }
//        dd($categories);
        return $categories;
    }
//  获取分类树状结构
    public function getTreeCategory()
    {
        $data = Category::get()->toArray();
        return Arr::tree($data,'title','id','parent_id');
    }
    public function parentName()
    {
       return $this->belongsTo(Category::class,'parent_id','id');
    }

    public function hasChild()
    {
        $data = $this->getTreeCategory();
        return Arr::hasChild($data,$this->id,'parent_id');
    }

//    找到当前分类下面的子分类方法
    public function category()
    {
        return $this->hasMany(Category::class,'parent_id','id');
    }

//    通过二级分类找到子类对应的商品数据
    public function getGoods()
    {
//        调用上面的方法找到二级分类的子类
        $sonCategory = $this->category()->limit(8)->get();
//        定义一个空的数组用来接收三级分类下的货品id
        $goodsId = [];
        foreach ($sonCategory as $category)
        {
//            先找到所有的商品数据
            $goods = Goods::orderBy('id','DESC')->get();
//            循环商品数据，判断商品的category_id数组中是否存在$category的id，如果存在，该商品就属于当前分类
            foreach ($goods as $good)
            {
                if (in_array($category['id'],$good['category_id']))
                {
                    $goodsId[] = $good['id'];
                    break;
                }
            }
        }
//        通过找到的goodId找到对应的商品数据，并返回
        $categoryGoods = Goods::whereIn('id',$goodsId)->get();
        return $categoryGoods;
    }

//    通过某一级分类找商品数据
   public function getCategoryGood()
   {
       $goods = Goods::get();
//       dd($goods);
       $goodIds = [];
       foreach ($goods as $good){
           if (in_array($this->id,$good['category_id'])){
               $goodIds[] = $good['id'];
           }
       }
//       dd($this->id);
       $categoryGood = Goods::whereIn('id',$goodIds)->paginate(12);
       return $categoryGood;
   }

//   获取当前类的父级分类
   public function getFatherCategory()
   {
       $data = $this->getTreeCategory();
       $fatherCategory = Arr::parentChannel($data, $this->id, 'id', 'parent_id');
//        参数                         说明
//        data                      	操作的数组
//        sid                      	    子栏目
//        fieldPri                 	    唯一键名，如果是表则是表的主键
//        fieldPid                  	父 ID 键名
//       将获取到的子分类数组反转并返回
       return array_reverse($fatherCategory);
   }
}
